//if the animal needs to move then this is called on
//this calls paranoia

//not eating, not dead
public class MoveHandler {
	static int X = -1;
	static int Y = -1;
	static double VELOCITY = -1;
	static int MAXSIZE = SimulationConstants.simsize - 1; //one less than map size

	public static boolean DEBUG = false;
	public static void setDEBUG(boolean b) {DEBUG = b;}
	public static boolean DEBUGA = false;
	public static void setDEBUGA(boolean b) {DEBUGA = b;}

	public void getAnimalTravelStats(AnimalObject animal){
		X = animal.getLocation().x;
		Y = animal.getLocation().y;
		VELOCITY = (double)animal.getVelocity();
	}

	public void doAnimalChasin(AnimalObject animal) {
		animal.setSearching(true);
		animal.setVelocity(animal.getMaxVelocity());
		//need direction


		//doMove();
	}

	public void doAnimalFlee(AnimalObject animal){
		animal.setFleeing(true);
		animal.setVelocity(animal.getMaxVelocity());
		//animal direction

		//need some kind of velocity calc here
		//so we can move an appropriate amount of x and y for time

		//doMove(); 
	}

	public void doAnimalUnFlee(AnimalObject animal) {
		animal.setFleeing(false); //no longer fleeing
		animal.setVelocity(0); //stop to catch breath
	}

	//hack for testing move
	//just became the official move function
	public static void doMove(loc whereto, AnimalObject animal) {
		//x and y here are input values that correspond to where we want to be
		//so now we have to figure out what that is in relation to us

		//this is where we are asolutely

		double x = (double)whereto.x;
		double y = (double)whereto.y;

		double myX = (double)animal.getLocation().x;
		double myY = (double)animal.getLocation().y;
		//so... where we want to move relative to us
		//
		double u = x - myX;
		double v = y - myY;

		//are we moving? we need to be
		if(VELOCITY == 0) {
			animal.setVelocity(1);
		} 

		//are we fleeing from something?
		if(animal.getFleeing() == true) {
			animal.setVelocity(animal.getMaxVelocity());
		}

		//get our velocity
		double velocity = (double)animal.getVelocity();
		//distance to destination
		double R = Math.sqrt(Math.pow(u,2) + Math.pow(v,2));
		//if our velocity is greater than our distance
		//our velocity becomes our distance... since like
		//we can most definately get there and we would stop when 
		//we did

		//unit vectors
		if(R>0)
		{
			u /= R;
			v /= R;

			//check distance, adjust so we dont move past where we want to go
			//we are basically always choosing the smaller radius
			if(velocity >= R)
				if(R>0)
					velocity = R;

			if(animal.getMigrate() == true)
				velocity = 15;

			double newx =  u * velocity;
			double newy =  v * velocity;

			newx = Math.round(newx + myX);
			newy = Math.round(newy + myY);

			//do not run off the map
			if( (int)newx < 0)
				newx = (MAXSIZE-newx)%MAXSIZE;
			if( (int)newx >MAXSIZE)
				newx = newx%MAXSIZE;
			if( (int)newy < 0)
				newy = (MAXSIZE-newy)%MAXSIZE;
			if( (int)newy > MAXSIZE)
				newy = newx%MAXSIZE;
			//so...animal change our location now

			animal.setLocation( (int)newx, (int)newy ); 
			animal.adjustEnergy((-1)*animal.getVelocity());
			if(SimulationConstants.sealevel > HeightMap.getHeightHere(animal.getLocation())) {
				animal.adjustEnergy((-1)*animal.getVelocity());
				//swimming hurts
				//if(animal.getFleeing() == true)
					//animal.adjustEnergy((-1)*animal.getVelocity());
				//swimming and fleeing is just... taxing
			}
			//if(animal.getFleeing() == true)
			//animal.ad

			if(DEBUG) {
				if(newx==0&&newy==0)
				{
					System.out.println("ohgod");
					System.out.println("we moved a distance of:"+velocity); 

					System.out.println("R is:"+R); 
				}

				System.out.println("we started here: "+myX+","+myY);
				System.out.println("we wanted to move here:"+x+","+y);
				System.out.println("we actually moved here:"+newx+","+newy);

				System.out.println("relative change: "+u+","+v);
			}
			animal.setMigrate(false);
			if(animal.getChasing()) {
				if(DEBUGA)
					System.out.println("The Second rule of fight club...");
				AttackDefenseHandler.doFight(animal, animal.getFocus());
				

			}
		}

	}

}
